function bindElements(){

	//bind submit button
	$("#Submit, #NoShow").bind('click', function(){
		if( this.id == 'NoShow' ){
			//if decline offer is clicked, the api.cfm see's the url var and declines the offer
			$("#frmStep3").attr('action', 'api.cfm?api=update_step3_employee&NoShow=1').submit();	
		}else{
			$("#frmStep3").attr('action', 'api.cfm?api=update_step3_employee').submit();
		}
		return false;
	});
	
	//datepicker replacement for cfcalendar.  Flash bad.  Calendar looks bad.
	$("#DateOfHire_datepicker").datepicker({
		onSelect: function(dateText, inst) { $("#DateOfHire").val(dateText); }									 
	}).datepicker("setDate", $("#DateOfHire").val());
	
	
	$("#DOB").datepicker({ 
		changeYear: true, changeMonth: true, yearRange: '1900:2010', autoSize: false,
		prevText: 'Earlier', nextText: 'Later', showOn: 'button', 
		buttonImage: '/CFIDE/scripts/ajax/resources/cf/images/DateChooser.png', buttonImageOnly: true,
		onClose: function(dateText, inst) { $(this).focus()}
	});

}


var validDate = function(date){
	//pauls date validator in action with jquery.
	var validator = new pdv();
	if (date > 0 || date.length == 10) {
		if( validator.IsSQLServerDate(date) ){				
			return true;
		}else{
			return false;	
		}		
	}
}



var validateElements = function(){
	//sets error count to zero, if greater then zero, some data is not valid
	var error_count = 0;

	//var for required element error message.  A custom message can 
	//be set on the input element called error_message
	var require_message = function(id){		
		return $("#" + id).attr('error_message');	
	}
	
	//these fields are require on form.
	var required_fields = ['DOB', 'SSN', 'DateOfHire'];
	
	//these fields need to be regex valildated.
	var re_ssn = ['SSN'];
	
	//regex validators
	var re = {SSN : '^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$'};
	
	//removes error messages from previous validation.
	$(".ui-state-error").removeClass();
	
	//creates html of errors
	var html_errors = function(){
		var error_link = "";		
		//if fields in the required_fields variable are empty this error is triggered.	checking for dob also.
		$.map(required_fields,function(field){
			if( !$("#" + field).val() && field != 'DOB' )	{
				error_count++;
				$("#" + field).addClass('ui-state-error');
				error_link = error_link + "<span class='link_error'" + "id='error_link_" + field + "'>" + 
				field + ":</span> " + require_message(field) + "<br />";
			
			}//massive truth table to check date is entered and valid format
			else if( (!$("#" + field).val() && field === 'DOB') || 
					  ($("#" + field).val() && field === 'DOB' && !validDate($("#" + field).val())) )
			{
				error_count++;
				$("#" + field).addClass('ui-state-error');
				error_link = error_link + "<span class='link_error'" + "id='error_link_" + field + "'>" + 
				field + ":</span> " + require_message(field) + "<br />";
			}
		});
		
		//regex fields format check, error is triggered if field is malformed
		$.map(re_ssn, function(field){
			if( $("#" + field).val() && !$("#" + field).val().match(new RegExp( re[field] )) ){
				error_count++;
				$("#" + field).addClass('ui-state-error');
				error_link = error_link + "<span class='link_error'" + "id='error_link_" + field + "'>" + 
				field + ":</span> " + require_message(field) + "<br />";
			}
		});
		
		return error_link;
	};
	
	//creates jquery ui dialog box html
	$("div.entry").append('<div id="error_dialog" style="width: auto;"><p>' + html_errors() + '</p></div>');
	
	//creates jquery ui dialog box object
	var $error_dialog = $('#error_dialog').dialog({
		autoOpen: false, width: 'auto', modal: true, title: 'The following fields have errors', resizable: false,
		open: function(event, ui) { $("div.ui-dialog-titlebar").width( $(".ui-dialog").width() ); },
		close: function(event, ui) {$(this).dialog( "destroy" ) }
	});

	//if no errors in validation, well... the data is valid!
	if(error_count){
		$error_dialog.dialog('open');
		$("span[id^='error_link_']").click(function(){
			var element_id = "#" + this.id.split("_")[2];			
			$.scrollTo(element_id);//jquery pluging ScrollTo
			$error_dialog.dialog('close');
			$(element_id).focus();
		});
		return false;
	}else{return true;}
};



//ajax POST to add to database.
function submitForm(obj, api, id, successFunc, completeFunc, errorFunc){	
	var api_url = "api.cfm?api=" + api + ( (id) ? "&id=" + id : "" );
	$.ajax({
		type:'POST', url:api_url, dataType: 'json', async: true, data: obj,
		success: function() { successFunc },
		complete: function(){ completeFunc },
		error: function(){ errorFunc }
	});
}



//ajax data get
var getData = function(api, id, successFunc, completeFunc, errorFunc){
	var data = "";
	var api_url = "api.cfm?api=" + api + "&" + id;	
	$.ajax({
		type:'GET', url:api_url, dataType: 'json', async: false,
		success: function(json){ data = json; },
		complete: function(){ completeFunc },
		error: function(){ errorFunc }
	});
	return data;
};



$(function(){
	bindElements();
});